package me.idashu.attg.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import me.idashu.attg.domain.CirlEntity;
import me.idashu.attg.domain.PageEntity;
import me.idashu.attg.service.CirlService;
import me.idashu.attg.util.SystemConfig;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

@Controller
@RequestMapping("/cirl")
public class CirlController {
    @Autowired
    private CirlService cirlService;
    
    @Autowired
    private SystemConfig systemConfig;
    
    @RequestMapping("/getListCirl")
    public String getListCirl(Model model,HttpServletRequest request){
    	try{
    		PageEntity pageEntity = new PageEntity();
    		CirlEntity cirlEntity = new CirlEntity();
    		selectListCirl(model, pageEntity, cirlEntity);
    	}catch(Exception ex){
    		ex.printStackTrace();
    		return "error";
    	}
    	return "cirl_list";
    }
    
    public void selectListCirl(Model model,PageEntity pageEntity,CirlEntity cirlEntity){
    	Map<String, Object> map = new HashMap<String, Object>();
    	int total = cirlService.getCountCirlEntity(cirlEntity);
    	pageEntity.setTotalPage(total);
		pageEntity.setPageLess(pageEntity.getPage());
    	map.put("pageEntity", pageEntity);
    	map.put("cirlEntity", cirlEntity);
    	List<CirlEntity> listCirl = cirlService.getAllCirlEntity(map);
    	model.addAttribute("cirllist", "圈子列表");
    	model.addAttribute("listCirl", listCirl);
    	model.addAttribute("pageEntity", pageEntity);
    }
    
    @RequestMapping("/cirlAjaxList")
    public String getAjaxCirlList(Model model,@ModelAttribute PageEntity pageEntity,@ModelAttribute CirlEntity cirlEntity){
    	try{
    		selectListCirl(model, pageEntity, cirlEntity);
    	}catch(Exception ex){
    		ex.printStackTrace();
    		return "error";
    	}
    	return "cirlAjaxlist";
    }
    
    @RequestMapping("/deleteCirl")
    public String deleteAjaxCirlist(Model model,@RequestParam("cirlId") int cirlId){
    	try{
    		PageEntity pageEntity = new PageEntity();
    		CirlEntity cirlEntity = new CirlEntity();
    		cirlService.deleteCirlEntity(cirlId);
    		selectListCirl(model, pageEntity, cirlEntity);
    	}catch(Exception ex){
    		ex.printStackTrace();
    		return "error";
    	}
    	return "cirlAjaxlist";
    }
    @RequestMapping("/addCirl")
    public String setCirl(Model model){
    	model.addAttribute("addCirl", "添加圈子");
    	return "add_cirl";
    }
    
    @RequestMapping("/addCirlInfo")
    public @ResponseBody String addCirlInfo(@ModelAttribute CirlEntity cirlEntity,
			@RequestParam(value = "file", required = false) MultipartFile file,Model model,HttpServletRequest request){
    	try{
			String dir = systemConfig.getDir("dir");
			String prodir = systemConfig.getDir("prodir");
			System.out.println("file------->:" + file);
			if (file.getOriginalFilename() != null
					&& !"".equals(file.getOriginalFilename())) {
				System.out.println("file:" + file.getOriginalFilename());
				try {
					InputStream is = file.getInputStream();
					FileInputStream fis = (FileInputStream) is;
					byte[] inOutb = new byte[fis.available()];
					String uuid = UUID.randomUUID().toString().replace("-", "");
					FileOutputStream fos = new FileOutputStream(new File(dir + uuid
							+ ".jpg"));
					cirlEntity.setCirl_imag(prodir + uuid + ".jpg");
					fis.read(inOutb);
					fos.write(inOutb);
					fis.close();
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			cirlEntity.setCirl_date(new Date());
			cirlEntity.setCirl_seq(0);
			if(cirlEntity.getMember_id()==-1){
				CirlEntity cirl = cirlService.getGongGaoCirl(cirlEntity.getMember_id());
				if(cirl==null){
					cirlService.insertCirlEntity(cirlEntity);
					model.addAttribute("success", "添加公告成功！");
				}else{
					model.addAttribute("success", "公告已存在，请删除后再添加！");
					return "gonggao";
				}
			}else{
				model.addAttribute("success", "添加圈子成功！");
				cirlService.insertCirlEntity(cirlEntity);

			}
    	}catch(Exception e){
    		e.printStackTrace();
    		return "error";
    	}
    	return "success";
    }
    
    @RequestMapping("/updateCirl")
    public String updateCirl(Model model,@RequestParam("cirlId") int cirlId){
    	try{
    		CirlEntity cirlEntity = cirlService.getOneCirlEntity(cirlId);
    		model.addAttribute("cirlEntity", cirlEntity);
    		model.addAttribute("updateCirl", "修改圈子信息");
    	}catch(Exception ex){
    		ex.printStackTrace();
    		return "error";
    	}
    	return "update_cirl";
    }
    
    @RequestMapping("/updateCirlInfo")
    public String updateCirlInfo(Model model,@ModelAttribute CirlEntity cirlEntity,@RequestParam(value = "file", required = false) MultipartFile file,HttpServletRequest request){
    	try{
    		String dir = systemConfig.getDir("dir");
			String prodir = systemConfig.getDir("prodir");
			System.out.println("file------->:" + file);
			if (file.getOriginalFilename() != null
					&& !"".equals(file.getOriginalFilename())) {
				System.out.println("file:" + file.getOriginalFilename());
				try {
					InputStream is = file.getInputStream();
					FileInputStream fis = (FileInputStream) is;
					byte[] inOutb = new byte[fis.available()];
					String uuid = UUID.randomUUID().toString().replace("-", "");
					FileOutputStream fos = new FileOutputStream(new File(dir + uuid
							+ ".jpg"));
					cirlEntity.setCirl_imag(prodir + uuid + ".jpg");
					fis.read(inOutb);
					fos.write(inOutb);
					fis.close();
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			cirlService.updateCirlEntity(cirlEntity);
			model.addAttribute("success", "修改圈子成功！");
    		model.addAttribute("updateCirl", "修改圈子信息");
    	}catch(Exception ex){
    		ex.printStackTrace();
    		return "error";
    	}
    	return "success";
    }
    
    @RequestMapping("/getGongGao")
    public String getGongGaoCirl(Model model){
    	try{
    		CirlEntity cirlEntity = cirlService.getGongGaoCirl(-1);
    		 model.addAttribute("listGongGao", "公告列表");
    		 model.addAttribute("cirlEntity", cirlEntity);
    	}catch(Exception ex){
    		ex.printStackTrace();
    		return "error";
    	}
    	return "gonggao_list";
    	
    }
    
    @RequestMapping("/deleteGongGao")
    public String deleteGongGaoCirl(Model model){
    	try{
    		cirlService.deleteGongGao();
    		CirlEntity cirlEntity = cirlService.getGongGaoCirl(-1);
    		 model.addAttribute("listGongGao", "公告列表");
    		 model.addAttribute("cirlEntity", cirlEntity);
    	}catch(Exception ex){
    		ex.printStackTrace();
    		return "error";
    	}
    	return "gonggao_list";
    	
    }
    
    @RequestMapping("/upCirl")
    public String upCirl(Model model,@RequestParam("cirlId") int cirlId){
    	try{
    		PageEntity pageEntity = new PageEntity();
    		CirlEntity cirlEntity = new CirlEntity();
    		List<CirlEntity> listCirl = cirlService.getCirlBySeq();
    		for (int i = 0; i < listCirl.size(); i++) {
    			CirlEntity cirl = listCirl.get(i);
				cirl.setCirl_seq(0);
				cirlService.updateCirlEntity(cirl);
			}
    		CirlEntity cirll = cirlService.getOneCirlEntity(cirlId);
    		cirll.setCirl_seq(1);
    		cirlService.updateCirlEntity(cirll);
    		selectListCirl(model, pageEntity, cirlEntity);
    	}catch(Exception ex){
    		ex.printStackTrace();
    		return "error";
    	}
    	return "cirlAjaxlist";
    	
    }
    
}
